package com.test.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class GraphicView extends SurfaceView
	implements Runnable, SurfaceHolder.Callback{
	protected SurfaceHolder surfaceHolder;
	protected Thread updateThread;
	protected int screenWidth;
	protected int screenHeight;
	protected long updateInterval;

	public GraphicView(Context context) {
		super(context);
		
		init(this);
	}

	public GraphicView(Context context, SurfaceView view){
		super(context);
		
		init(view);
	}

	private void init(SurfaceView view){
        // surfaceHolderの取得
        surfaceHolder = view.getHolder();
        surfaceHolder.addCallback(this);

        // 画面描画間隔の設定
        updateInterval = getUpdateInterval();
        
        setClickable(true);
	}
	
	// 画面描画間隔を返す
	public long getUpdateInterval(){
		return 100;
	}
	// SurfaceViewオーバーライドメソッド
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		screenWidth = width;
		screenHeight = height;
	}
	public void surfaceCreated(SurfaceHolder holder) {
		updateThread = new Thread(this);
		updateThread.start();
	}
	public void surfaceDestroyed(SurfaceHolder holder) {
		updateThread = null;
	}
	
	// 更新用メソッド
	public void run() {
		while(updateThread != null){
			Canvas canvas = surfaceHolder.lockCanvas();
			drawCanvas(canvas);
			surfaceHolder.unlockCanvasAndPost(canvas);

			try{
				Thread.sleep(updateInterval);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	// 画面描画
	public void drawCanvas(Canvas canvas){
		canvas.drawColor(Color.DKGRAY);
	}

	public boolean onTouchUp(MotionEvent event){
		return false;
	}
	public boolean  onTouchDown(MotionEvent event){
		return false;
	}
	public boolean onTouchMove(MotionEvent event){
		return false;
	}
	public void onOptionMenu(){
	}
}
